package com.gcl.rabbitmq;

import com.gcl.rabbitmq.utils.RabbitmqUtils;
import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Consumer01 {

    private static final String QUEUE_NAME = "Hello Word";

    public static void main(String[] args) throws IOException, TimeoutException {

        Channel channel = RabbitmqUtils.getChannel();
        System.out.println("C1 等待接受消息");


        // 不公平分发消息 (能者多劳)
//        channel.basicQos(1);

        // 阈取值
        channel.basicQos(2);

        DeliverCallback deliverCallback = (consumeTag, delivery) ->{
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("接收到的消息 "+ new String(delivery.getBody()));

            // 手动应答
            /**
             * 1. 消息的标记
             * 2. 是否批量应答信道中的消息
             */
            channel.basicAck(delivery.getEnvelope().getDeliveryTag(),false);

        };


        CancelCallback cancelCallback = (consumerTag) -> {
            System.out.println("参数接受失败");
        };
        channel.basicConsume(QUEUE_NAME,false,deliverCallback,cancelCallback);
    }
}
